package com.yls.autoapply.rest;

import com.yls.autoapply.model.ServiceParam;
import com.yls.autoapply.model.ServiceResult;
import com.yls.autoapply.model.SshResponse;
import com.yls.autoapply.server.NodeServer;
import com.yls.autoapply.service.AutoApplyService;
import com.yls.autoapply.util.ServerUtil;
import com.yls.autoapply.util.ServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class AutoApplyRest {

    @Autowired
    private AutoApplyService autoApplyService;

    @GetMapping("/hello")
    public SshResponse hello(@RequestParam("cmd") String cmd) {
        NodeServer nodeServer = new NodeServer();
        SshResponse response = null;
        try {
            nodeServer.auth(ServerUtil.get("node"));
            boolean b = nodeServer.rm(cmd);
            System.out.println(b);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            nodeServer.close();
        }
        return response;
    }

    @PostMapping("/list")
    public List<ServiceResult> autoapply(@RequestBody List<ServiceParam> params) {
        List<String> nameSpaces = Arrays.asList("qz4auat1", "qz4auat2");
        params.forEach(x -> {
            if (x.getNamespaces() == null || x.getNamespaces().isEmpty()) {
                x.setNamespaces(nameSpaces);
            }
        });
        Set<String> allkey = ServiceUtil.getAllkey();
        List<ServiceParam> normalKeys = params.stream().filter(x -> allkey.contains(x.getService())).collect(Collectors.toList());
        List<ServiceParam> notKeys = params.stream().filter(x -> !allkey.contains(x.getService())).collect(Collectors.toList());
        List<ServiceResult> results = autoApplyService.autoApply(normalKeys);
        for (ServiceParam param : notKeys) {
            ServiceResult result = new ServiceResult();
            result.setService(param.getService());
            result.setVersion(param.getVersion());
            result.setNamespaces(param.getNamespaces());
            result.setResult("fail");
            result.setMsg("不存在该service");
            results.add(result);
        }
        return results;

    }

    @GetMapping("/single")
    public List<ServiceResult> autoapply(@RequestParam("service") String service,
                                         @RequestParam("version") String version, @RequestParam(value ="namespace", defaultValue = "qz4auat1,qz4auat2")String namespace) {
        ServiceParam param = new ServiceParam();
        param.setService(service);
        param.setVersion(version);
        param.setNamespaces(Arrays.asList(namespace.split(",")));
        ArrayList<ServiceParam> params = new ArrayList<>();
        params.add(param);
        return autoapply(params);

    }

    @GetMapping("/image")
    public List<String> getImage(@RequestParam("grep") String grep) {
        return autoApplyService.getImage(grep);
    }

    @GetMapping("/version")
    public List<String> version(@RequestParam(value = "namespace", defaultValue = "qz4auat1") String namespace) {
        return autoApplyService.getVersion(namespace);
    }

    @GetMapping("/basic")
    public Map<String, List<String>> basic() {
        return ServiceUtil.basic();
    }

    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        log.info(file.getOriginalFilename());
        if (!file.getOriginalFilename().endsWith(".war")) {
            return "文件格式不正确";
        }
        return autoApplyService.upload(null, file);
    }


}
